package com.junci.mtest.views.activity;

import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider;

import com.junci.mtest.R;
import com.junci.mtest.databinding.ActivityMainBinding;
import com.junci.mtest.utils.LogUtil;
import com.junci.mtest.utils.MainTabView;
import com.junci.mtest.views.BaseActivity;
import com.junci.mtest.views.BaseFragment;
import com.junci.mtest.views.fragment.HohoFragment;
import com.junci.mtest.views.fragment.MainLessonFragment;
import com.junci.mtest.views.fragment.MainFragment;
import com.junci.mtest.views.fragment.MineFragment;
import com.junci.mtest.vm.MainViewModle;


/**
 * Created by YM
 * Create Date 2024/4/30 18:22
 * Used 首页
 */
public class MainActivity extends BaseActivity {
    enum Tabs
    {
        //tab标识
        MAIN,KE,HOho,MINE;
        private int id = 0;
        private BaseFragment fragment ;
        public BaseFragment getFragment() {
            return fragment;
        }
        public void setFragment(BaseFragment fragment) {
            this.fragment = fragment;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
    }
    MainViewModle viewModle;
    //页卡
    private boolean isSaveInstanceState = false;
    int lastTag = 0; //当前标识位
    ActivityMainBinding binding ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        viewModle = new ViewModelProvider(this).get(MainViewModle.class);
        binding.setViewModel(viewModle);
        binding.setLifecycleOwner(this);
        binding.executePendingBindings();
        isSaveInstanceState = savedInstanceState != null ? true : false;
        if (isSaveInstanceState) {
            lastTag = savedInstanceState.getInt("lastTag");
        } else {
            lastTag = R.id.tab_main;
        }
        initTabFragments();
    }


    private void initTabFragments(){
        /*
           首页 [MainFragment]
           课程 [MainLessonFragment]
           Hoho [HohoFragment]
           我的 [MineFragment]
         */
        Tabs.MAIN.setId(R.id.tab_main);
        Tabs.KE.setId(R.id.tab_ke);
        Tabs.HOho.setId(R.id.tab_hoho);
        Tabs.MINE.setId(R.id.tab_mine);
        Tabs.MAIN.setFragment(MainFragment.newInstance());
        Tabs.KE.setFragment(MainLessonFragment.newInstance());
        Tabs.HOho.setFragment(HohoFragment.newInstance());
        Tabs.MINE.setFragment(MineFragment.newInstance());
        FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
        try {
            tr.add(R.id.rl_main,Tabs.MINE.fragment, Tabs.MINE.id + "")
                        .hide(Tabs.MINE.fragment)
                        .add(R.id.rl_main,Tabs.HOho.fragment, Tabs.HOho.id+"")
                        .hide(Tabs.HOho.fragment)
                        .add(R.id.rl_main, Tabs.KE.fragment, Tabs.KE.id+"")
                        .hide(Tabs.KE.fragment)
                        .add(R.id.rl_main, Tabs.MAIN.fragment, Tabs.MAIN.id + "").commit();
            } catch (Exception e) {
            }
//        }
        binding.tabMain.setOnClickListener(new myTabclick(Tabs.MAIN));
        binding.tabKe.setOnClickListener(new myTabclick(Tabs.KE));
        binding.tabHoho.setOnClickListener(new myTabclick(Tabs.HOho));
        binding.tabMine.setOnClickListener(new myTabclick(Tabs.MINE));
        lastTag = Tabs.MAIN.id;
        switchFragment(Tabs.MAIN);
    }
    private void setChecked(int position) {
        try {
            ((MainTabView) findViewById(lastTag)).setChecked(false);
            ((MainTabView) findViewById(position)).setChecked(true);
        } catch (NullPointerException exception) {
            exception.printStackTrace();
        }
    }

    class myTabclick implements View.OnClickListener{
        private Tabs tabs;
        public myTabclick(Tabs tabs) {
            this.tabs = tabs;
        }

        @Override
        public void onClick(View v) {
            switchFragment(tabs);
        }
    }

    private void switchFragment(Tabs tabs) {
        if (tabs.id!=lastTag) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.hide(getSupportFragmentManager().findFragmentByTag(lastTag + ""))
                    .show(tabs.fragment).commit();
            setChecked(tabs.id);
            lastTag = tabs.id;
        }
    }


}
